package com.glodon.ggzy.fwpt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.core.utils.QuerySupport;
import com.glodon.ggzy.entity.GgJsLaw;
import com.glodon.ggzy.entity.GgTdGg;
import com.glodon.ggzy.entity.GgTdGs;
import com.glodon.ggzy.entity.GgTdGsItem;
import com.glodon.ggzy.entity.GgTdLaw;
import com.glodon.ggzy.entity.GgTdProjItem;
import com.glodon.ggzy.entity.GgTdProject;
import com.glodon.ggzy.fwpt.dao.IGgTdGgDao;
import com.glodon.ggzy.fwpt.dao.IGgTdGsDao;
import com.glodon.ggzy.fwpt.dao.IGgTdLawDao;
import com.glodon.ggzy.fwpt.dao.IGgTdProjItemDao;
import com.glodon.ggzy.fwpt.dao.IGgTdProjectDao;
import com.glodon.ggzy.fwpt.service.ITdfcProjectService;
import com.glodon.ggzy.vo.ZbgsResultVO;

/**
 * 
 * 土地房产处理Service实现类
 * @author yangjf
 * @date 2013/06/11
 */
@Service
public class TdfcProjectServiceImpl implements ITdfcProjectService {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IGgTdProjectDao ggTdProjectDao;

	/**
	 * 获取信息公开土地房产分页数据
	 * @param queryModel
	 * @return
	 */
	@Override
	public Page queryXxgkTdfcPage(PageSearchModel queryModel) {
		return ggTdProjectDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Autowired
	private IGgTdGgDao ggTdGgDao;

	/**
	 * 获取信息公开土地房产分页数据
	 * @param queryModel
	 * @return
	 */
	@Override
	public Page queryZbggTdfcPage(PageSearchModel queryModel) {
		return ggTdGgDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Autowired
	private IGgTdGsDao ggTdGsDao;

	/**
	 * 获取中标公示-土地房产分页数据
	 * @param queryModel
	 * @return
	 */
	@Override
	public Page queryZbgsTdfcPage(PageSearchModel queryModel,String gsName,String prjName) {

		StringBuffer hql = new StringBuffer();
		StringBuffer countHql = new StringBuffer();
		hql.append("select project,g from GgTdProject project , GgTdGs g where project.id=g.prjId ");
		countHql.append("select count(*) from GgTdProject project, GgTdGs g where project.id=g.prjId  ");
		
		Map<String,Object > parameterMap = new HashMap<String,Object>();
		
		if(!StringUtils.isEmpty(gsName)){
			hql.append(" and g.gsName like :gsName");
			countHql.append(" and g.gsName like :gsName");
			parameterMap.put("gsName", gsName);
		}
		
		if(!StringUtils.isEmpty(prjName)){
			hql.append(" and project.prjName like :prjName");
			countHql.append(" and project.prjName like :prjName");
			parameterMap.put("prjName", prjName);
		}
		return ggTdGsDao.pageQuery(hql.toString(), parameterMap, countHql.toString(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	/**
	 * 获取 出让信息 分页数据
	 * @param queryModel
	 * @return
	 */
	@Override
	public Page queryCrrTdfcPage(PageSearchModel queryModel) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, GgTdProject.class);
		queryModel.addParamToMap("crrNo", queryModel.getOrgCode());
		return ggTdProjectDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	/**
	 * 获取 交易信息  分页数据
	 * @param queryModel
	 * @return
	 */
	@Override
	public Page queryCrrJyxxTdfcPage(PageSearchModel queryModel) {
		return ggTdProjectDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}
	
	@Autowired
	private IGgTdProjItemDao ggTdProjItemDao;
	
	/**
	 * 根据项目id 获取 土地房产出让明细(gg_td_proj_item)--具体标的
	 * @param prjId
	 * @return
	 */
	@Override
	public List<GgTdProjItem> queryGgTdProItemsById(String prjId) {
			DetachedCriteria criteria = DetachedCriteria.forClass(GgTdProjItem.class);
			criteria.add(Restrictions.eq("prjId", prjId));
			List<GgTdProjItem> list = ggTdProjItemDao.findByDetachedCriteria(criteria);
			return list;
	}
	
	
	/**
	 * 根据项目id 取具体公告信息
	 * @param prjId
	 * @return
	 */
	@Override
	public GgTdGg queryGgTdGgByProId(String prjId) {
		
			DetachedCriteria criteria = DetachedCriteria.forClass(GgTdGg.class);
			criteria.add(Restrictions.eq("id", prjId));
			List<GgTdGg> list = ggTdGgDao.findByDetachedCriteria(criteria);
			GgTdGg ggTdGg = new GgTdGg();
			for (int i = 0; i < list.size(); i++) {
			    ggTdGg = list.get(0);
				break;
			}
			
			return ggTdGg;
	}
	
	/**
	 * 根据id 取具体公告信息
	 * @param prjId
	 * @return
	 */
	@Override
	public GgTdGg queryGgTdGgById(String id) {
		
			DetachedCriteria criteria = DetachedCriteria.forClass(GgTdGg.class);
			criteria.add(Restrictions.eq("id", id));
			List<GgTdGg> list = ggTdGgDao.findByDetachedCriteria(criteria);
			GgTdGg ggTdGg = new GgTdGg();
			for (int i = 0; i < list.size(); i++) {
			    ggTdGg = list.get(0);
				break;
			}
			
			return ggTdGg;
	}

	
	/**
	 * 根据项目id 取公示信息
	 * @param prjId
	 * @return
	 */
	@Override
	public GgTdGs queryGgTdGsByProId(String prjId) {
		
			DetachedCriteria criteria = DetachedCriteria.forClass(GgTdGs.class);
			criteria.add(Restrictions.eq("prjId", prjId));
			List<GgTdGs> list = ggTdGsDao.findByDetachedCriteria(criteria);
			GgTdGs ggTdGs = new GgTdGs();
			for (int i = 0; i < list.size(); i++) {
			    ggTdGs = list.get(0);
				break;
			}
			
			return ggTdGs;
	}
	
	/**
	 * 根据 id 取公示信息
	 * @param prjId
	 * @return
	 */
	@Override
	public GgTdGs queryGgTdGsById(String id) {
		
			DetachedCriteria criteria = DetachedCriteria.forClass(GgTdGs.class);
			criteria.add(Restrictions.eq("id", id));
			List<GgTdGs> list = ggTdGsDao.findByDetachedCriteria(criteria);
			GgTdGs ggTdGs = new GgTdGs();
			for (int i = 0; i < list.size(); i++) {
			    ggTdGs = list.get(0);
				break;
			}
			
			return ggTdGs;
	}
	
	/**
	 * 获取中标公示-土地房产
	 * @param queryModel
	 * @return
	 */
	@Override
	public List<ZbgsResultVO> queryZbgsResultList(String proId) {

		StringBuffer hql = new StringBuffer();
		hql.append("select item,g from GgTdProjItem item , GgTdGsItem g where item.id=g.priItemId  ");
		
		if (proId != null) {
			hql.append(" and item.prjId =?");
		}
		List<Object[]> find = ggTdGgDao.find(hql.toString(),proId);
		
		List<ZbgsResultVO> result = new ArrayList<ZbgsResultVO>();
		
		for (int i = 0; i < find.size(); i++) {
			ZbgsResultVO zbgsResultVO = new ZbgsResultVO();
			result.add(zbgsResultVO);
			Object[]object= find.get(i);
			GgTdProjItem ggTdProjItem = (GgTdProjItem)object[0];
			
			GgTdGsItem ggTdGsItem = (GgTdGsItem)object[1];
			
			String gpxhZdbh = ggTdProjItem.getGpxhZdbh();
			
			String zlwz = ggTdProjItem.getZlwz();
			String area = ggTdProjItem.getArea();
			String ghyt = ggTdProjItem.getGhyt();
			String rjl  = ggTdProjItem.getRjl();
			String crnx = ggTdProjItem.getCrnx();
			
			String jmrName = ggTdGsItem.getJmrName();
			Double cjCprice = ggTdGsItem.getCjCprice();
			
			zbgsResultVO.setZlwz(zlwz);
			zbgsResultVO.setArea(area);
			zbgsResultVO.setGhyt(ghyt);
			zbgsResultVO.setRjl(rjl);
			zbgsResultVO.setCrnx(crnx);
			zbgsResultVO.setJmrName(jmrName);
			zbgsResultVO.setCjCprice(cjCprice);
			zbgsResultVO.setGpxhZdbh(gpxhZdbh);
		}
		
		return result;
	}
	
	@Autowired
	private IGgTdLawDao lawDao;
	
	/**
	 * @param queryModel
	 * @param lawName
	 * @return
	 */
	@Override
	public Page queryTdfcLaw(PageSearchModel queryModel, String lawName) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, GgTdLaw.class);
		queryModel.addParamToMap(QuerySupport.ORDERBYDESC, "updatetime");
		if (!StringUtils.isEmpty(lawName)) {
			queryModel.addParamToMap("name_like", "%" + lawName + "%");
		}
		return lawDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());

	};
	
	@Override
	public GgTdLaw getGgTdLawById(String id) {
		return lawDao.get(id);
	};


}
